package com.inforich.disposal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.utils.StringUtils;
import com.inforich.projectManage.domain.ProjectRubbishExpand;
import com.inforich.projectManage.domain.ProjectRubbishRemoval;
import com.inforich.projectManage.domain.ProjectRubbishReport;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import com.inforich.projectManage.service.IProjectRubbishRemovalService;
import com.inforich.projectManage.service.IProjectRubbishReportService;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.disposal.mapper.DisposalLandMapper;
import com.inforich.disposal.domain.DisposalLand;
import com.inforich.disposal.service.IDisposalLandService;

/**
 * 处置地Service业务层处理
 *
 * @author hjm
 * @date 2022-10-25
 */
@Service
public class DisposalLandServiceImpl extends
    ServiceImpl<DisposalLandMapper, DisposalLand> implements IDisposalLandService {

  @Autowired
  private IProjectRubbishExpandService projectRubbishExpandService;
  @Autowired
  private IProjectRubbishRemovalService projectRubbishRemovalService;
  @Autowired
  private IProjectRubbishReportService projectRubbishReportServices;

  @Override
  public Page<DisposalLand> pageDisposalLand(Page<DisposalLand> page, Map<String, Object> map) {
    return baseMapper.pageDisposalLand(page, map);
  }

  @Override
  public List<DisposalLand> listDisposalLand(Map<String, Object> map) {
    return baseMapper.listDisposalLand(map);
  }

  @Override
  public DisposalLand getDisposalLandById(Long id) {
    return baseMapper.selectDisposalLandById(id);
  }

  @Override
  public String checkData(DisposalLand disposalLand) {
    int count = count(new LambdaQueryWrapper<DisposalLand>()
        .eq(DisposalLand::getName, disposalLand.getName())
        .ne(disposalLand.getId() != null, DisposalLand::getId, disposalLand.getId()));
    return count > 0 ? "企业名称重复" : "";
  }

  @Override
  public int insertDisposalLand(DisposalLand disposalLand) {
    return baseMapper.insertDisposalLand(disposalLand);
  }

  @Override
  public int updateDisposalLand(DisposalLand disposalLand) {
    return baseMapper.updateDisposalLand(disposalLand);
  }

  @Override
  public AjaxResult removeDisposalLand(Long id) {
    int count1 = projectRubbishExpandService.count(new QueryWrapper<ProjectRubbishExpand>()
        .eq("disposal_land_id", id));
    if (count1 > 0) {
      return AjaxResult.error("该企业已有新扩改项目使用，不可删除");
    }
    int count2 = projectRubbishRemovalService.count(new QueryWrapper<ProjectRubbishRemoval>()
        .eq("disposal_land_id", id));
    if (count2 > 0) {
      return AjaxResult.error("该企业已有拆迁项目使用，不可删除");
    }
    int count3 = projectRubbishReportServices.count(new QueryWrapper<ProjectRubbishReport>()
        .eq("disposal_land_id", id));
    if (count3 > 0) {
      return AjaxResult.error("该企业已有固废申报使用，不可删除");
    }
    removeById(id);
    return AjaxResult.success();
  }


}
